/*
 * 22/10/2011 - 15:21
 */
package org.nettyflux.jsonrpc;

import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;

/**
 * @author Victor Williams Stafusa da Silva
 */
public class JsonMarshal {

    private final ObjectMapper mapper;

    public JsonMarshal() {
        this(new ObjectMapper());
    }

    public JsonMarshal(ObjectMapper mapper) {
        this.mapper = mapper;
    }

    public byte[] write(Object obj) throws IOException {
        return mapper.writeValueAsBytes(obj);
    }

    public <E> E read(Class<E> targetType, byte[] bytes) throws IOException {
        return mapper.readValue(bytes, targetType);
    }
}
